package com.huawo.opensdk.utils;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtils {
    private static final String TAG = "FileUtils";
    private static final String ZIP_FOLDER_NAME = "music/mp3";

    public static String compressFilesToZip(Context context, List<String> filePaths) throws Exception {
        String targetZipPath = context.getFilesDir().getAbsolutePath() + "/target.zip";
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(targetZipPath);
            zos = new ZipOutputStream(fos);

            for (String filePath : filePaths) {
                File file = new File(filePath);
                if (file.exists()) {
                    if (file.isFile()) {
                        zipFile(file, zos);
                    } else if (file.isDirectory()) {
                        zipDirectory(file, zos, "");
                    }
                } else {
                    Log.e(TAG,"File does not exist: " + filePath);
                }
            }

            Log.e(TAG, "Files compressed to: " + targetZipPath);
        } catch (Exception e) {
            Log.e(TAG,"是否处于OTA状态==compressFilesToZip===catch");
            throw e;
        } finally {
            if (zos != null) {
                zos.closeEntry();
                zos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }

        return targetZipPath;
    }

    private static void zipFile(File file, ZipOutputStream zos) throws IOException {
        FileInputStream fis = new FileInputStream(file);

        String zipFilePath = ZIP_FOLDER_NAME + "/" + file.getName();

        ZipEntry zipEntry = new ZipEntry(zipFilePath);
        zos.putNextEntry(zipEntry);

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = fis.read(buffer)) != -1) {
            zos.write(buffer, 0, bytesRead);
        }

    }

    private static void zipDirectory(File directory, ZipOutputStream zos, String parentPath) throws IOException {
        File[] files = directory.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    zipFile(file, zos);
                } else if (file.isDirectory()) {
                    String directoryPath = parentPath + file.getName() + "/";
                    zipDirectory(file, zos, directoryPath);
                }
            }
        }
    }

    public static String getFolderPath(String filePath) {
        int lastSeparatorIndex = filePath.lastIndexOf('/');
        if (lastSeparatorIndex != -1) {
            return filePath.substring(0, lastSeparatorIndex);
        }
        return "";
    }
}
